<template>
  <div style="text-align:center;width:100%;margin:0 auto;height:93px;padding-top:18px;background:#979fa7">
    <div style="float:left"><img :src="logo"></div>
    <div>
      <h1>
        <font>联合重卡IT综合下载平台</font>
      </h1>
    </div>
  </div>
   <div class="role-container">
    <div class="role-tree-left flex-col">
      <div class="title"><i class="el-icon-user"></i>下载导航</div>
      <el-scrollbar class="el-role-list">
        <el-tree
          :data="tree"
          @node-click="nodeClick"
          :default-expand-all="false"
          node-key="id"
          :default-expanded-keys="openKeys"
          :expand-on-click-node="true"
          style="padding: 5px 0; margin-right: 2px"
        >
          <template #default="{ data }">
            <div class="action-group">
              <div class="action-text">
                {{ data.value }}
              <a @click="all_download(data)">打包下载</a>
            </div>
            </div>
          </template>
        </el-tree>
      </el-scrollbar>
    </div>
    <div class="drap-line"></div>
    <div class="role-tree-right flex-col">
      <el-tabs ref="el_tabs" type="border-card" class="role-tree-right-tabs" style="height: 100%;" v-model="activeName" @tab-click="handleTabClick">
        <el-tab-pane name="download-sort">
          <template #label>
            <span class="download-sort">
              <el-icon><FolderOpened /></el-icon>
              <span>下载分类</span>
            </span>
          </template>
            <div style="text-align:center;width:100%;margin:0 auto;height:60px;padding-top:18px;">
              <el-form :inline="true" :model="formInline" class="demo-form-inline">
                <el-form-item label="查询范围">
                  <el-select v-model="formInline.sort" @change="sortChange">
                    <el-option v-for="item in primaryOptions" :key="item.value" :label="item.label" :value="item.value"/>
                  </el-select>
                </el-form-item>
                <el-form-item label="关键词">
                  <el-input v-model="formInline.title"></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary" @click="onSubmit">查询</el-button>
                </el-form-item>
              </el-form>
            </div>
            <div>
              <vol-table :ck="false" ref="tableList" @rowClick="rowClick" :loadKey="true" :columns="columns" :pagination-hide="false" :height="getTableListHeight()" :defaultLoadPage="true" @loadBefore="loadBefore" @loadAfter="loadTableAfter" :endEditAfter="endEditAfter" url="/api/Ok3w_Article/getPageData" :row-index="true"  :index="true" click-edit="false" ></vol-table>
            </div>
        </el-tab-pane>
        <el-tab-pane name="download-chart">
          <template #label>
            <span class="download-chart">
              <el-icon><TrendCharts /></el-icon>
              <span>数据统计</span>
            </span>
          </template>
          <el-tabs tab-position="left" class="download-chart-sort" @tab-click="handleTabChartClick">
            <el-tab-pane label="文档占比">
              <div class="download-chart-content" id="documentRateChart"></div>
            </el-tab-pane>
            <el-tab-pane label="文档下载量">
              <div class="download-chart-content" id="downloadCountChart"></div>
            </el-tab-pane>
            <el-tab-pane label="用户上传排行">
              <div class="download-chart-content" id="uploadRankChart"></div>
            </el-tab-pane>
          </el-tabs>
        </el-tab-pane>
      </el-tabs>
    </div>
  </div>


</template>

<script>
import { ref, onMounted, onUnmounted } from 'vue';
import http from "@/../src/api/http.js";
var imgUrl = require("@/assets/imgs/comlog.png");
import VolHeader from "@/components/basic/VolHeader.vue";
import VolTable from "@/components/basic/VolTable.vue";
import look_downfile from "@/views/JRdownfile/look_downfile.vue";
import {useRouter} from "vue-router";
import {ElMessage,ElMessageBox} from 'element-plus';

// let router=useRouter();
// const router=useRouter();
var echarts = require("echarts");
let $downloadCountChart,$uploadRankChart,$documentRateChart
import {downloadCountChart,uploadRankChart,documentRateChart } from "./jr_downfile_chart_options.js";
export default {
  components: { VolHeader, VolTable, look_downfile, http },
  data() {
    return {
      //注意:数据源中必须有id与parentId字段
      //*树形tree需要注意Id与parentId循环依赖的问题 */
      tree_source :[
        { name: 'tree1', id: 1, parentId: 0 },
        { name: 'tree2', id: 2, parentId: 0 },
        { name: 'tree3', id: 3, parentId: 1 },
        { name: 'tree4', id: 4, parentId: 1 },
        { name: 'tree5', id: 5, parentId: 7 },
        { name: 'tree6', id: 6, parentId: 7, hidden: true },
        { name: 'tree8', id: 8, parentId: 3 },
      ],
      tree:null,
      lcout:3,//每行显示的数量
      formInline: {
        title: "",
      },
      columns: [{ field: 'ok3w_article_id', title: '上传文章主键id', type: 'guid', width: 110, hidden: true, require: true, align: 'left' },
      { field: 'ClassID', title: '分类', type: 'int', bind: { key: '下载分类', data: [] }, width: 80, require: true, align: 'left', sort: true },
      { field: 'SortPath', title: '目录短路径', type: 'string', width: 110, hidden: true, align: 'left' },
      { field: 'OutLink', title: '外发链接', type: 'string', width: 380, align:'left',clickEdit:true},
      { field: 'Title', title: '标题', type: 'string', width: 190, align: 'left' },
      { field: 'TitleColor', title: '文章颜色', type: 'string', width: 110, hidden: true, align: 'left' },
      { field: 'TitleURL', title: '文章路径', type: 'string', width: 110, hidden: true, align: 'left' },
      { field: 'AttachmentURL', title: '下载', type: 'file', width: 170, align: 'left' },
      { field: 'Content', title: '内容', type: 'string', width: 110, hidden: true, align: 'left' },
      { field: 'Author', title: '上传人', type: 'string', width: 120, hidden: true, align: 'left' },
      { field: 'ComeFrom', title: '上传来自于', type: 'string', width: 120, hidden: true, align: 'left' },
      { field: 'AddTime', title: '上传时间', type: 'datetime', width: 150, hidden: true, align: 'left', sort: true },
      { field: 'Inputer', title: '插入人', type: 'string', width: 120, hidden: true, align: 'left' },
      { field: 'IsPic', title: '图片列表', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'PicFile', title: '文档图片', type: 'string', width: 220, hidden: true, align: 'left' },
      { field: 'IsPass', title: '是否审核', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'IsTop', title: '是否置顶', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'IsDelete', title: '是否删除', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'IsCommend', title: '是否推荐', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'IsMove', title: '是否滚动', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'IsPlay', title: '是否轮播', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'IsUserAdd', title: '上传人', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'GiveJifen', title: '作者积分', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'vUserGroupID', title: '游客权限', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'vUserMore', title: '游客登记', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'vUserJifen', title: '游客积分', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'Hits', title: '查看数', type: 'int', width: 110, hidden: true, align: 'left' },
      { field: 'id', title: '文章id', type: 'int', width: 80, hidden: true, require: true, align: 'left' },
      { field: 'ChannelID', title: '栏目编号 1：软件管理栏，2：相关说明栏', hidden: true, type: 'int', width: 80, align: 'left' },
      { field: 'CreateID', title: '创建人id', type: 'int', width: 80, hidden: true, align: 'left' },
      { field: 'Creator', title: '创建人名称', type: 'string', width: 90, align: 'left' },
      { field: 'CreateDate', title: '创建时间', type: 'datetime', width: 150, align: 'left', sort: true },
      { field: 'ModifyID', title: '修改人id', type: 'int', width: 80, hidden: true, align: 'left' },
      { field: 'Modifier', title: '修改人名称', type: 'string', width: 90, align: 'left' },
      { field: 'ModifyDate', title: '修改时间', type: 'datetime', width: 150, align: 'left', sort: true },
      { field: 'downloadCount', title: '下载次数', hidden: true, type: 'int', width: 80, align: 'left',sort:true },],
      logo: ref(imgUrl),
      fl_types: [
        { ok3w_article_id: 1, text: 'aaa1' },

      ],
      all_list:[],
      list: [
        { title: '框架2.x版本不支持直接升级Vue3版本(代码生成器已修改)' },

        //(框架内部组件vue2/3语法都在使用),你也可以使用vue3语法
      ],
      nodeClassID:null,
      primaryOptions:[{value:'标题',label:'标题'},{value:'内容',label:'内容'}],
      activeName:"download-sort",
    };
  },

  setup() {
    let open = (item) => {
      window.open(item.url, '_blank');
    };
    let interval;
    onMounted(() => {

    });
    onUnmounted(() => {
      interval && clearInterval(interval);

    });
    return { open};
  },

  mounted() {
    //console.log(Math.ceil(this.fl_types_1.length/3));
    let qid = "";
    var turl = "/api/Ok3w_Article/get_ok3w_for_front?qid=" + qid + "&lstype=all";
    http.post(turl, {}, true).then((s) => {
      this.fl_types = s.online_class_list;
      this.list = s.alltable;
      this.alllist = s.alltable;
      // console.log(this.list);
      this.tree_source=s.tree_data;
      this.tree = this.base.convertTree(this.tree_source, (node, data, isRoot) => {
        //设定其他字段属性
        //node当前遍历的节点：{ name: 'tree1', id: 1, parentId: 0 }
        //data整个数据源
        //isRoot是否为根节点，一级节点

        //添加一些别的属性
        node.value = node.name;
        node.label = node.name;
        node.text = node.name;
        node.classid = node.classid;
      });
    });

    this.dragControllerDiv();
    this.formInline.sort="标题";
    this.getUploadRankInfo();
    this.getDocumentRate();
    this.getDownloadCount();

    var chartDom=document.getElementsByClassName('download-chart-content');
    var rightDom=document.getElementsByClassName('role-tree-right');
    for(let i=0;i<chartDom.length;i++){
      chartDom[i].style.height=rightDom[0].offsetHeight-65+'px';
      chartDom[i].style.width=rightDom[0].offsetWidth-180+'px';
    }
  },

  methods: {
    loadBefore(param,callBack){
      if(this.formInline.title.length > 0){
        if(this.formInline.sort=="内容"){
          param.wheres=[];
          param.wheres.push(...[{name:"Content",value:this.formInline.title,displayType:"like"}]);
        }else{
          param.wheres=[];
          param.wheres.push(...[{name:"Title",value:this.formInline.title,displayType:"like"}]);
        }
        callBack(true);
      }
      if(!(this.nodeClassID === null)){
        param.wheres.push(...[{name: "ClassID", value: this.nodeClassID}]);
        callBack(true);
      }
    },

    rowClick({ row, column, event }) {
      if(column.property!='OutLink'){
      //  navigator.clipboard.writeText(row.OutLink).then(()=>{
      //    this.$message.success('外发链接复制成功');
      //  })
      // }
      // else{
        let id=row.id;
        if(column.property=='AttachmentURL' && event.target.className == 't-file'){
          let url="api/Ok3w_Article/updateDownloadCount?id="+id;
          http.post(url,{},true);
        }
        window.open(row.OutLink,'_blank');
      }
    },

    nodeClick(node, selected){
      this.activeName="download-sort";
      this.nodeClassID=node.classid;
      this.formInline.sort="标题";
      this.formInline.title="";
       let params = {
        page: 1, //分页页数(可不填)
        rows: 30, //分页大小(可不填)
        sort: "title", //可不填
        order: "desc/asc", //可不填
        wheres:[
          { name: "ClassID", value: this.nodeClassID },
      /* { name: "ClassID", value: '30,31',displayType:"selectList" },  */
             ],  };
        this.$refs.tableList.load(params, true); //查询审核页的业务数据

    },

    onSubmit() {
      let params = {
        page: 1, //分页页数(可不填)
        rows: 30, //分页大小(可不填)
        sort: "title", //可不填
        order: "desc/asc", //可不填
        wheres:[{ name: "Title", value: this.formInline.title,displayType:"like" },
              ],  
    };
   // params.value="out";//说明从系统外登录
    this.$refs.tableList.load(params, true); //查询审核页的业务数据
  },

  getTableListHeight(){
    return document.documentElement.clientHeight-276;
  },

  //调整下载导航宽度
  dragControllerDiv(){
    var drapLineDom=document.getElementsByClassName('drap-line');
    var leftDom=document.getElementsByClassName('role-tree-left');
    var rightDom=document.getElementsByClassName('role-tree-right');
    var containerDom=document.getElementsByClassName('role-container');
    //鼠标按下事件
    drapLineDom[0].onmousedown=function(e){
      drapLineDom[0].style.background='#818181';
      var startX=e.clientX;
      drapLineDom[0].left=drapLineDom[0].offsetLeft;
      //鼠标拖动事件
      document.onmousemove=function(e){
        var endX=e.clientX;
        var moveLen=drapLineDom[0].left+(endX-startX);
        var maxWidth=containerDom[0].clientWidth-drapLineDom[0].offsetWidth;//界面最大宽度
        if (moveLen<230){//下载导航最小宽度
          moveLen=230;
          return;
        }
        if (moveLen>maxWidth-780){//下载导航最大宽度
          moveLen=maxWidth-780;
          return;
        }
        drapLineDom[0].style.left=moveLen;
        leftDom[0].style.width=moveLen-10+'px';
        //不修改display属性，会在拖动过程中出现拖拽条消失情况（拖拽条宽度变成0）
        rightDom[0].style.display=flex;
        rightDom[0].style.width=(containerDom[0].clientWidth-moveLen-10)+'px';
      };
      //鼠标松开事件
      document.onmouseup=function(e){
        drapLineDom[0].style.background='#f6f6f6';
        document.onmousemove=null;
        document.onmouseup=null;
        drapLineDom[0].releaseCapture && drapLineDom[0].releaseCapture();
      };
      drapLineDom[0].setCapture && drapLineDom[0].setCapture();
      return false;
    };
  },

    sortChange(){
      let params = {
        wheres:[{ name: "", value: "",displayType:"like" },],  
      };
      this.formInline.title="";
      this.$refs.tableList.load(params, true);
    },

    //文档占比
    getDocumentRate(){
      let url="api/Ok3w_Article/getDocumentRate";
      http.post(url,{},true).then((s)=>{
        var data=new Array();
        s.forEach((e)=>{
          if(e.parentID===0){
            var value=e.value;
          var name=e.name;
          data.push({value,name});
          }
        });
        documentRateChart.series[0].data.push(...data);
        $documentRateChart=echarts.init(document.getElementById('documentRateChart'));
        $documentRateChart.setOption(documentRateChart,true);
      });
    },

    //下载量排行
    getDownloadCount(){
      let url="api/Ok3w_Article/getDownloadCount";
      http.post(url,{},true).then((s)=>{
        var countData=new Array();
        var titleData=new Array();
        s.forEach((e)=>{
          countData.push(e.downloadCount);
          titleData.push(e.title);
        });
        downloadCountChart.yAxis.data.push(...titleData);
        downloadCountChart.series[0].data.push(...countData);
        $downloadCountChart=echarts.init(document.getElementById('downloadCountChart'));
        $downloadCountChart.setOption(downloadCountChart,true);
      });
    },

    //文档上传量排行
    getUploadRankInfo(){
      let url="api/Ok3w_Article/getUploadRankInfo";
      http.post(url,{},true).then((s)=>{
        var creatorData=new Array();
        var uploadData=new Array();
        s.forEach((e)=>{
          creatorData.push(e.creator);
          uploadData.push(e.num);
        });
        uploadRankChart.yAxis.data.push(...creatorData);
        uploadRankChart.series[0].data.push(...uploadData);
        $uploadRankChart=echarts.init(document.getElementById('uploadRankChart'));
        $uploadRankChart.setOption(uploadRankChart,true);
      });
    },

    handleTabClick(){
      this.handleTabChartClick();
    },

    handleTabChartClick(){
      downloadCountChart.yAxis.data=[];
      uploadRankChart.yAxis.data=[];
      documentRateChart.series[0].data=[];
      downloadCountChart.series[0].data=[];
      uploadRankChart.series[0].data=[];
      this.getDocumentRate();
      this.getDownloadCount();
      this.getUploadRankInfo();
    },

    //点击下载导航中的下载按钮，出现消息弹出框，提示是否下载当前节点下的所有文件，并进行后续操作
    all_download(data){
      ElMessageBox.confirm(
        '是否要全部下载 '+data.value+' 目录下的所有文件？',
        '提示',
        {
          confirmButtonText:'确认',
          cancelButtonText:'取消',
          type:'info',
        }
      )
      .then(()=>{
        ElMessage({
          type:'success',
          message:'开始下载',
        })
        this.all_download_files(data.classid,data.name);
      })
      .catch(()=>{
        ElMessage({
          type:'info',
          message:'放弃下载',
        })
      })
    },

    //下载指定节点下的所有文件
    all_download_files(classid,name){
      let url="api/Ok3w_Article/create_zip_rfqfiles?ClassID="+classid;
      this.http.post(url,{},"正在导出数据...",{responseType:"blob"}).then((content)=>{
        debugger
        const blob=new Blob([content]);
        if("download" in document.createElement("a")){
          const elink=document.createElement("a");
          elink.download=name+".zip";
          elink.style.display="none";
          elink.href=URL.createObjectURL(blob);
          document.body.appendChild(elink);
          elink.click();
          URL.revokeObjectURL(elink.href);
          document.body.removeChild(elink);
        }else{
          navigator.msSaveBlob(blob,"导出文件非IE.xlsx");
        }
      })
    },
  },
  
  destroyed() {
  }
};

</script>

<style>
a:hover{
  cursor: pointer;
}

.el-row {
  margin-bottom: 10px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.bg-purple-dark {
  background: #99a9bf;
}
.bg-purple {
  background: #d3dce6;
}
.bg-purple-light {
  background: #e5e9f2;
}
.grid-content {
  border-radius: 4px;
  min-height: 30px;
}
.row-bg {
  padding: 5px 0;
  background-color: #f9fafc;
}
.text {
  font-size: 14px;
}

.item {
  margin-bottom: 18px;
}

.clearfix:before,
.clearfix:after {
  display: table;
  content: "";
}
.clearfix:after {
  clear: both;
}

.box-card {
  width: 350px;
}
</style>
<style lang="less" scoped>
.role-container {
  position: absolute;
  background: #f6f6f6;
  top:93px;
  width: 100%;
  left:0;
  bottom: 0;
  padding: 10px;
  display: flex;
  .flex-col {
    display: flex;
    flex-direction: column;
  }
  .role-tree-left {
    border: 1px solid #f2f2f2;
    background: #fff;
    width: 230px;
    margin-right: 10px;
    .title {
      i {
        margin-left: 10px;
      }
    }
  }
  .role-tree-right {
    background: #fff;
    border: 1px solid #f2f2f2;
    width: 0;
    flex: 1;
    .title {
      display: flex;
      i {
        margin-left: 10px;
      }
      div {
        flex: 1;
      }
    }
    .action-group {
      display: flex;
      // line-height: 32px;
      justify-content: center;
      align-items: center;
      label {
        float: left;
      }
      .action-text {
        line-height: 33px;
        label {
          margin-right: 5px;
        }
      }
    }
  }
  .title {
    padding: 10px;
    background: rgb(246 250 255);
    font-weight: bold;
    font-size: 14px;
    letter-spacing: 2px;
  }
  .el-role-list {
    flex: 1;
    height: 0;
    overflow-x: hidden;
  }
}
.role-tree-left ::v-deep(.el-tree-node__content) {
  cursor: pointer;
  height: auto;
  padding: 5px;
  margin: 2px 10px;
  font-size: 15px;
  position: relative;
  a{
    position: absolute;
    display: none;
    right:10px;
  }
}
.role-tree-left ::v-deep(.el-tree-node__content:hover) {
  background: #f4f4f4;
  border-radius: 20px;
  a{
    display:inline-block;
    color:aqua;
  }
}
.role-tree-left ::v-deep(.is-current > .el-tree-node__content:first-child) {
  background: #f2f2f2;
  border-radius: 20px;
}
.role-tree-right ::v-deep(.el-tree-node__content) {
  margin-bottom: 5px;
  height: auto;
}
.role-tree-right ::v-deep(.el-checkbox__label) {
  position: relative;
  top: 2px;
}

.role-tree-right-tabs{
  .el-icon{
    vertical-align: middle;
  }
  span{
    vertical-align: middle;
    // margin-left: 4px;
    padding: 4px;
    font-weight: bold;
    font-size: 14px;
    letter-spacing: 2px;
    color:#000;
  }
}

.drap-line{
  cursor:col-resize;
  position: relative;
  border-radius: 2px;
  width:5px;
}
</style>
